package whu.o2o.connection;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

import whu.o2o.View.LoadDialog;


public class UploadAudioTask extends AsyncTask<Params, String, String> {
	public String _result = "";
	public String connectInfo;
	int responseCode;
	boolean isTimeOut;
	Handler handler;
	Activity context;
	String strResult = "";
	Params[] params;

	/**
	 * Params数组包括url，requestType请求类型，params请求参数
	 * 
	 * @param params
	 */
	public void newConnectTask(Params[] params, Activity context, Handler handler) {
		this.context = context;
		this.handler = handler;
		this.params = params;
		context.runOnUiThread(new Runnable() {

			public void run() {
				LoadDialog.showDailog(UploadAudioTask.this.context, "录音已成功，正在上传录音...");
			}
		});
		this.execute(params);
	}

	@Override
	protected String doInBackground(Params... params) {
		// TODO Auto-generated method stub
		Params p = params[0];
		HttpClient httpClient = null;
		try {
			HttpPost post = new HttpPost(p.getUrl());
			HttpConnectionParams.setConnectionTimeout(post.getParams(), 20 * 1000);
			HttpConnectionParams.setSoTimeout(post.getParams(), 60 * 1000);
			List<NameValuePair> list = new ArrayList<NameValuePair>();
			list.add(new BasicNameValuePair("xml", p.getXml()));
			post.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
			httpClient = new DefaultHttpClient();
			// 请求HttpClient，取得HttpResponse
			HttpResponse httpResponse = httpClient.execute(post);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				// 取得返回的字符串
				strResult = EntityUtils.toString(httpResponse.getEntity());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strResult;
	}

	@Override
	protected void onCancelled() {
		// TODO Auto-generated method stub
		super.onCancelled();
	}

	@Override
	protected void onPostExecute(String result) {
		context.runOnUiThread(new Runnable() {

			public void run() {
				// TODO Auto-generated method stub
				if (context != null && !context.isFinishing()) {
					LoadDialog.closeDialog();
				}
			}
		});
		// TODO Auto-generated method stub
		Message msg = Message.obtain();
		msg.obj = result;
		handler.sendMessage(msg);
		super.cancel(true);
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
	}

	@Override
	protected void onProgressUpdate(String... values) {
		// TODO Auto-generated method stub
		super.onProgressUpdate(values);
	}

}
